﻿//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 文件名: DatUser.cs
// 作 者：代码自动生成
// 日 期：2011-10-26 11:07:14
// 描 述：
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00             
//==================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Text;
using Csla;
using lite;
using Csla.Validation;
using Csla.Data;
using System.Data.SqlClient;
using ZX.DAL;
using OpenExpressApp.ORM;
using ZX.Common;
using Csla.Core;
using System.Reflection;
using System.Text.RegularExpressions;

namespace ZX.EHR.Library
{
    /// <summary>
    /// 
    /// </summary>
	public partial class DatUser
	{
        /// <summary>
        /// 保存用户验证数据
        /// </summary>
        /// <returns></returns>
        public override string CheckData()
        {
            if (this.LoginName == "")
            {
                return "登录用户名不能为空!";
            }
            else if (this.Password.Length != "202cb962ac59075b964b07152d234b70".Length)
            {
                this.Password = ExtendMethods.GetMd5Hash("123456");
            }
            if (this.Email != null && !Regex.IsMatch(this.Email, @"^\s*([A-Za-z0-9_-]+(\.\w+)*@([\w-]+\.)+\w{2,3})\s*$"))
            {
                return "邮箱格式不合法!";
            }
            //if (!Regex.IsMatch(this.EffectDate.ToShortDateString(), @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$"))
            //{
            //    return "生效日期格式不合法!";
            //}
            //else if (!Regex.IsMatch(this.ExpireDate.ToShortDateString(), @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$"))
            //{
            //    return "失效日期格式不合法!";
            //}

            if (this.UserID ==Guid.Empty)
            {
                object instance = Common.Reflection.CreateInstance("ZX.BLL.BUser", "ZX.BLL");
                Type t = instance.GetType();
                object obj = t.InvokeMember("IsExist", BindingFlags.Default | BindingFlags.InvokeMethod
                            , null, instance, new object[] { this.LoginName });

                if ((bool)obj)
                {
                    return "用户名已存在!";
                }
                else
                {
                    return IsOutsiteExist();
                }
            }
            else
            {
                return IsOutsiteExist();
            }
        }
        private string IsOutsiteExist()
        {
            string temp = "";
            if (this.IsOutsite == true)
            {
                string test = this.IsDefaultRoleName;
                if (this.CustomerID == Guid.Empty)
                {
                    temp = "客户不能为空!";
                }
                else if (this.UName == "")
                {
                    temp = "学员名称不能为空!";
                }
                else
                {
                    //object instance = Common.Reflection.CreateInstance("ZX.BLL.BUser", "ZX.BLL");
                    //Type t = instance.GetType();
                    //object objDatUser = t.InvokeMember("GetDatUserByID", BindingFlags.Default | BindingFlags.InvokeMethod
                    //        , null, instance, new object[] { this.CustomerID, this.EmpID, this.LoginName, true });
                    //if ((bool)objDatUser)
                    //{
                    //    temp = "该人员已被选择,不能重复选择!";
                    //}
                    //else
                    {
                        DateTime EffectDate = this.EffectDate; //生效日期
                        DateTime ExpireDate = this.ExpireDate; //失效日期
                        DateTime LocalDate = DateTime.Now;   //当前日期
                        if (LocalDate > EffectDate && LocalDate < ExpireDate)
                        {
                            this.IsExpire = false; //0 设置可用
                        }
                        else
                        {
                            this.IsExpire = true; //1 设置失效
                        }
                        this.CreateDate = DateTime.Now;
                    }
                }
            }
            else
            {
                if (this.EmpID == Guid.Empty)
                {
                    temp = "职员不能为空!";
                }
                else
                {
                    object instance = Common.Reflection.CreateInstance("ZX.BLL.BUser", "ZX.BLL");
                    Type t = instance.GetType();
                    object objDatUser = t.InvokeMember("GetDatUserByID", BindingFlags.Default | BindingFlags.InvokeMethod
                            , null, instance, new object[] { this.CustomerID, this.EmpID, this.UserID, false });
                    if ((bool)objDatUser)
                    {
                        temp = "该职员已被选择,不能重复选择!";
                    }
                    else
                    {
                        object instanceLoginName = Common.Reflection.CreateInstance("ZX.BLL.BUser", "ZX.BLL");
                        Type tLoginName = instanceLoginName.GetType();
                        object objDatUserLoginName = tLoginName.InvokeMember("IsExist", BindingFlags.Default | BindingFlags.InvokeMethod
                            , null, instanceLoginName, new object[] { this.LoginName, this.UserID });
                        if ((bool)objDatUserLoginName)
                        {
                            temp = "该用户登录名已经存在!";
                        }
                        else {
                            DateTime EffectDate = this.EffectDate; //生效日期
                            DateTime ExpireDate = this.ExpireDate; //失效日期
                            DateTime LocalDate = DateTime.Now;   //当前日期
                            if (LocalDate > EffectDate && LocalDate < ExpireDate)
                            {
                                this.IsExpire = false; //0 设置可用
                            }
                            else
                            {
                                this.IsExpire = true; //1 设置失效
                            }
                            this.CreateDate = DateTime.Now;
                        }
                    }
                }
            }
            return temp;
        }
        /// <summary>
        /// 判断用户名是否已存在
        /// </summary>
        /// <param name="entity">用户实体对象</param>
        /// <returns>true:已存在 false:不存在</returns>
        public bool IsExist(string loingName)
        {
            return true;
        }


	}
}

